#!/usr/bin/perl -w
use strict;
use DBI;
use DBD::Pg;
use FindBin;
use lib ("/net/cpp-group/Leo/bin", "$FindBin::Bin/..");
use Spreadsheet::WriteExcel::Big;
use axes;

#
#	Create workbook
#
	
	my $workbook  = Spreadsheet::WriteExcel::Big->new("analysis.xls");
	die "Problems creating new Excel file: $!" unless defined $workbook;
	#
	#	cell formats
	#
	my $bold_format = $workbook->addformat();
		$bold_format->set_bold();
		$bold_format->set_color('blue');
		$bold_format->set_align('right');
		$bold_format->set_num_format('0.00;0.00');		
	
	my $fixed_point_format = $workbook->addformat();
		$fixed_point_format->set_align('right');
		$fixed_point_format->set_num_format('0.00');
		
	my $right_format = $workbook->addformat();
		$right_format->set_align('right');
	
	my $odd_format = $workbook->addformat();
		$odd_format->set_align('right');
		$odd_format->set_num_format('0.00');
		$odd_format->set_bg_color(43);


for my $file (@ARGV)
{
	my $file_name =($file =~/([^\/\.]+).all_nodes.list/) ? $1 : "default";
	
	
		
		
		my %all_nodes;
	
	
	#
	#	Save and sort all nodes
	#
	{
		open ALL_NODES, $file or die"Error\n\tCould not open $file.\n$@";
		while (<ALL_NODES>)
		{
			chomp;
			my ($to_leaf, undef, undef, $node_type) = split /\t/;
			next unless defined $node_type && $to_leaf =~ /[\d\.]+/;
			push(@{$all_nodes{$node_type}}, $to_leaf);
		}
		for (keys %all_nodes)
		{
			$_ = -$_ for (@{$all_nodes{$_}});
			@{$all_nodes{$_}} = sort {$a <=> $b} @{$all_nodes{$_}};
		}
	}
	
	
	
	
	#
	#	write raw data
	#
	{
		my $raw_data_worksheet = $workbook->addworksheet("$file_name.raw_data");
		
	
		#
		#	write header
		#
	
	
		my $col = 0;
		my $row = 7;
		$raw_data_worksheet->set_column('A:E',  26);
		$raw_data_worksheet->write_row($row++, 0, ["Ancestral node", "Mouse node", "Rat node", "Orth", "Mouse Orphan", "Rat Orphan"], $bold_format);
		
		my @cnts;
		for my $node_type("Ancestral Node", "Ortholog Tax1 Node", "Ortholog Tax2 Node", "Ortholog Node", "Orphan Tax1 Node", "Orphan Tax2 Node")
		{
	#	    next if $node_type =~ /orphan/i;
			$raw_data_worksheet->write_col($row, $col++, $all_nodes{$node_type}, $fixed_point_format);
			push(@cnts, scalar @{$all_nodes{$node_type}});
		}
		$raw_data_worksheet->write_col(0, 0, ["Ancestral nodes", "Mouse nodes", "Rat nodes", "Ortholog nodes", "Mouse Orphans", "Rat Orphans"], $bold_format);
		$raw_data_worksheet->write_col(0, 1, \@cnts);
		
	
	}
	{
		my $bins_worksheet = $workbook->addworksheet("$file_name.binned_data");
		$bins_worksheet->set_column('A:F',  26);
		$bins_worksheet->write_row(0, 0, ['bin', 'Ancestral duplications', 'Mouse specific duplications', 'Rat specific duplications', 'Duplications at speciation'], $bold_format);
		
		
		my ($graphmax, $graphmin, $interval) = ( 0, -2.0, 0.02);
		#	bin
		#
		my $col = 1;
		my $row = 1;
		my $cnt_bins = ($graphmax - $graphmin) / $interval + 1;
		#for my $node_type(keys %all_nodes)
		my @bin_names;
		for (my $i = 0; $i < $cnt_bins; ++$i)
		{
			push(@bin_names, $graphmin + $interval * $i);
		}
		$bins_worksheet->write_col(1, 0, \@bin_names, $bold_format);
		for my $node_type("Ancestral Node", "Ortholog Tax1 Node", "Ortholog Tax2 Node", "Ortholog Node")
		{
		
			my @bins =(0) x $cnt_bins;
			$bins_worksheet->write_col($row, $col, \@bins, $right_format);
			for my $datum(@{$all_nodes{$node_type}})
			{
				if ($datum < $graphmax && $datum >= $graphmin)
				{
					$bins[int(($datum - $graphmin) / $interval)]++;
				}
			}
			for (@bins)
			{
				last if ($_);
				$_ = "";
			}
			pop(@bins);
			$bins_worksheet->write_col($row, $col++, \@bins, $right_format);
	#		$bins_worksheet->write_col($row, $col - 1, [1,2,3], $right_format);
		}
	}
}
$workbook->close();

